<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('服务2列表')" />
    <style>
        .styled-table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
            font-size: 16px;
            font-family: Arial, sans-serif;
            text-align: left;
        }
        .styled-table thead tr {
            background-color: #009879;
            color: #ffffff;
            text-align: left;
            font-weight: bold;
        }
        .styled-table th, .styled-table td {
            padding: 12px 15px;
            border: 1px solid #dddddd;
        }
        .styled-table tbody tr {
            border-bottom: 1px solid #dddddd;
        }
        .styled-table tbody tr:nth-of-type(even) {
            background-color: #f3f3f3;
        }
        .styled-table tbody tr:hover {
            background-color: #f1f1f1;
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <input type="hidden" name="reqId" th:value="${reqId}">
                    <ul>
                        <li>
                            <label>名称：</label>
                            <input type="text" name="serName"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <!--<a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:service2:edit">
                <i class="fa fa-edit"></i> 修改
            </a>-->
            <a class="btn btn-danger" onclick="closeItem()">
                <i class="fa fa-reply-all"></i> 关闭
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:request:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:request:remove')}]];
    var statusDatas = [[${@dict.getType('sys_service_status')}]];
    var expStatusDatas = [[${@dict.getType('auto_exp_status')}]];
    var yesnoStatusDatas = [[${@dict.getType('sys_yn_status')}]];
    var approveFlag = [[${@permission.hasPermi('system:request:approve')}]];
    var prefix = ctx + "system/request2";
    var downloadUrl = prefix + "/download";
    var previewUrl = ctx + "common/preview";

    $(function() {
        var options = {
            url: prefix + "/serlist",
            createUrl: prefix + "/serAdd",
            updateUrl: prefix + "/serEdit/{id}",
            removeUrl: prefix + "/serRemove",
            modalName: "服务",
            columns: [{
                checkbox: true
            },
                {
                    field: 'infId',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'expcode',
                    title: 'expcode',
                    visible: false
                },
                {
                    field: 'sample.samName',
                    title: '样品名称'
                },
                {
                    field: 'sample.samSlot',
                    title: 'slot',
                    align: 'center',
                    visible: false
                },
                {
                    field: 'serName',
                    title: '服务名称'
                },
                {
                    field: 'svtypeName',
                    title: '服务类型',
                    visible: false
                },
                {
                    field: 'instrument.instName',
                    title: '仪器'
                },
                {
                    field: 'createTime',
                    title: '创建日期',
                    visible: false
                },
                {
                    field: 'templateName',
                    title: '模板'
                },
                {
                    field: 'sample.solvent',
                    title: '溶剂'
                },
                {
                    field: 'priority',
                    title: '优先',
                    visible: false,
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(yesnoStatusDatas, value);
                    }
                },
                {
                    field: 'process',
                    title: '状态',
                    align: 'center',
                    // visible: false,
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(expStatusDatas, value) + ' ' + $.table.selectDictLabel(statusDatas, row.serStatus);
                    }
                },
                {
                    field: 'acqupara',
                    title: '参数',
                    align: 'center',
                    formatter: function(value, row, index) {
                        if(row.acqupara == null){
                            return '<a href="javascript:void(0)"></a>';
                        }

                        // return '<a href="'+downloadUrl+'?id='+row.nmrFile.fileId+'"><i class="fa fa-search"></i></a>';
                        return '<a href="javascript:void(0)" title="查看参数信息" onclick="detail(\'' + value + '\')"><i class="fa fa-search"></i></a>';
                    }
                },
                {
                    field: 'startTime',
                    title: '开始时间',
                    visible: false
                },
                {
                    field: 'endTime',
                    title: '结束时间',
                    visible: false
                },
                {
                    field: 'nmrFile.fileName',
                    title: 'Result',
                    formatter: function(value, row, index) {
                        //验证状态非AM角色不可看
                        var html = [];
                        // html.push('<a href="#" onclick="showDocument(\'' + row.infId + '\')"><i class="fa fa-search"></i> Document</a><br/>');
                        if(row.nmrFile == null){
                            //显示信息
                            return row.resMsg;
                        }else {
                            html.push('<a href="'+downloadUrl+'?id='+row.nmrFile.fileId+'"><i class="fa fa-download"></i> Raw data</a>');
                            if(row.reportFiles != null && row.reportFiles.length > 0){
                                html.push('<br/>');
                                for(var i = 0; i < row.reportFiles.length; i++){
                                    //在新界面打开pdf文件，
                                    html.push('<a href="'+previewUrl+'?id='+row.reportFiles[i].fileId+'" target="_blank" style="text-decoration: underline;"> PDF report</a>');
                                    if(i < row.reportFiles.length - 1){
                                        html.push('<br/>');
                                    }
                                }
                            }
                        }
                        return html.join('');
                    }
                },
                {
                    field: 'zknjServiceApprove.approveStatus',
                    title: '验证信息',
                    align: 'center',
                    formatter: function(value, row, index) {
                        if(row.zknjServiceApproves == null){
                            return '<a href="javascript:void(0)"></a>';
                        }
                        return '<a href="javascript:void(0)" title="查看签名信息" onclick="esignline(\'' + row.infId + '\')"><i class="fa fa-search"></i></a>';
                    }
                },
                {
                    title: '操作',
                    // align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        var stop = '<a class="btn btn-danger btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="stop(\'' + row.infId + '\')"><i class="fa fa-stop"></i> 停止</a> ';
                        // 上传知识文档
                        var upload = '<a class="btn btn-info btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="upload(\'' + row.infId + '\')"><i class="fa fa-upload"></i> 文档</a> ';
                        var editFalse = '<a disabled="disable" class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)"><i class="fa fa-edit"></i>编辑</a> ';
                        var cancel = '<a class="btn btn-warning btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="cancel(\'' + row.infId + '\')"><i class="fa fa-remove"></i> 取消</a> ';
                        var removeFalse = '<a disabled="disable" class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)"><i class="fa fa-remove"></i>删除</a> ';
                        var verify = '<a class="btn btn-info btn-xs ' + approveFlag + '" href="javascript:void(0)" onclick="verify(\'' + row.infId + '\')"><i class="fa fa-sliders"></i>验证</a> ';

                        if(row.process != 0 && row.process != 1 && row.process != 11 && row.process != 12){       //Editable、Canceled、Done、Error
                            // actions.push(stop);
                        }
                        if(row.process == 11){
                            actions.push(verify);
                        }
                        if(row.process == 11 && row.serStatus == 5){
                            actions.push(cancel);
                        }
                        // actions.push(upload);
                        return actions.join('');
                    }
                }
            ]
        };
        $.table.init(options);
    });

    function detail(value) {
        // Split the parameter string into key-value pairs
        var params = value.split("|");
        var tableContent = "<table class='styled-table'><thead><tr><th>参数名</th><th>值</th></tr></thead><tbody>";

        params.forEach(function(param) {
            var keyValue = param.split("=");
            if (keyValue.length === 2) {
                tableContent += "<tr><td>" + keyValue[0] + "</td><td>" + keyValue[1] + "</td></tr>";
            }
        });

        tableContent += "</tbody></table>";

        // Display the formatted table in a modal
        $.modal.alertSuccess(tableContent);
    }



    function verify(infId){
        $.modal.open("服务结果验证", prefix + "/verify?infId="+infId, '770', '380');
    }

    function stop(infId){
        $.modal.confirm("确认停止实验吗？", function() {
            $.operate.post(prefix + "/serStop", { "infId": infId});
        })
    }

    function cancel(infId){
        $.modal.confirm("确认取消实验验证吗？取消后可再次提交验证", function() {
            $.operate.post(prefix + "/cancelVerify", { "infId": infId});
        })
    }

    function esignline(infId){
        $.modal.open2("验证信息", prefix + "/esignline?infId="+infId, ['关闭'], '770', '580');
    }

    function esign(content, type) {
        if(type == 6){
            $.modal.alertSuccess(content);
        } else if (type == 7) {
            $.modal.alertError(content);
        }
    }

    function esignatureContent(row) {
        var approver = row.zknjServiceApprove.approveBy;
        var approveStatus = row.zknjServiceApprove.approveStatus == 6?'Approved':'Not Approved';
        var reason = row.zknjServiceApprove.approveComment || '无';
        return `
        <div style="padding: 10px;">
            <p><strong>签名人：</strong>${approver}</p>
            <p><strong>状态：</strong>${approveStatus}</p>
            <p><strong>意见：</strong>${reason}</p>
        </div>
    `;
    }
</script>
</body>
</html>